import {Map} from 'immutable'

const user = (state = Map( {
  isLogin: localStorage.getItem( 'isLogin' ) === 'true',
  token: localStorage.getItem( 'token' ) === 'true',
  adminname: localStorage.getItem( 'adminname' ) || '',
  role: localStorage.getItem( 'role' ) * 1 || 1
} ), action) => {
  switch (action.type) {
    case 'CHANGE_LOGIN_STATE':
      return state.set( 'isLogin', action.payload )
    case 'CHANGE_TOKEN_STATE':
      return state.set( 'token', action.payload )
    case 'CHANGE_ADMINNAME_STATE':
      return state.set( 'adminname', action.payload )
    case 'CHANGE_ROLE_STATE':
      return state.set( 'role', action.payload )
    default:
      return state
  }
}

export default user
